*	************************************************************************
* 	File-Name: 	replication.do
*	Log-file:	na
*	Date:  		05/22/2020
*	Author: 	Jennifer Richmond, Shalu Agrawal, and Johannes Urpelainen
*	Data Used:  Shalu Agrawal; Nidhi Bali; Johannes Urpelainen; Aseem Mahajan; 
*				Daniel Robert Thomas; Sidhartha Vermani; Ryan Kennedy; Smart 
*				Power India; Initiative for Sustainable Energy Policy, 2019, 
*				"Rural Electricity Demand in India (REDI)"
*	Output		na
*	Purpose:   	replication .do file for "Drivers of household appliance usage: 
*				evidence from rural India." Energy for Sustainable Development.
*	************************************************************************


*	************************************************************************
*	0. Setting up the data
*	************************************************************************	
* Basic settings 
set more off, perm
capture log close
clear all
set type double

* Change this file path to the working directory where the dataset is located
use "C:\Users\Jen\Dropbox\Rockefeller Analysis\Manuscript_Jen\Input.dta", clear
* Change this file path to your working directory where you want to work throughout this file
cd "C:\Users\Jen\Dropbox\Rockefeller Analysis\Manuscript_Jen\Dataverse  "

*** Importing cleaned data ***
use "$input/cleandata", clear

*	************************************************************************
*	1. Descriptive statistics and figures (other figures created in R file)
*	************************************************************************

* TABLE 2: SUMMARY STATISTICS

estpost sum new_usage lighting_usage cooling_usage entertain_usage housekeep_usage grid shs mg backup totalapps higher_ed basic_ed q214_hh_expenses ln_expend q204_adults q206_children q201_gender hindu sched_caste q302_grid_years dist ln_dist q308_grid_hours q322_shs_hours q331_battery_hours q341_mgrid_hours q346_dg_hours avail avail_vmean q353_elec_satisfied_ salaried q218_land_ownership cost 

esttab . using "$tables\sumstats.tex", cell((count(label(N)) mean(label(Mean) fmt(2)) sd(label(Std. Dev.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)))) title("Summary statistics") label nomtitle nonumber replace varwidth(25) booktabs noobs

* FIGURE 3: SCATTERPLOT OF APPLIANCE USAGE AND ELECTRICITY AVAILABILITY

twoway sc lighting_usage_vmean avail_vmean, mcolor("191 230 249") graphregion(c(white)) xscale(lc(gray)) yscale(lc(gray)) title("Lighting", color(gray) size(medium)) ytitle(, c(gray) size(small)) xtitle("")/*"Years of electrification (mean by village)", c(gray) size(small))*/ ylabel(, labcolor(gray) tlc(gray) nogrid) xlabel(, labcolor(gray) tlc(gray) nogrid) ytitle("") /*"Number of Tier 1 appliances (mean by village)")*/ legend(off) || lfit lighting_usage_vmean avail_vmean, lc(gs12) saving(lighting2, replace)
	
graph2tex, epsfile(sc_lighting_avail)

twoway sc cooling_usage_vmean avail_vmean, mcolor("191 230 249") graphregion(c(white)) xscale(lc(gray)) yscale(lc(gray)) title("Cooling", color(gray) size(medium)) ytitle(, c(gray) size(small)) xtitle("")/*"Years of electrification (mean by village)", c(gray) size(small))*/ ylabel(, labcolor(gray) tlc(gray) nogrid) xlabel(, labcolor(gray) tlc(gray) nogrid) ytitle("") /*"Number of Tier 1 appliances (mean by village)")*/ legend(off) || lfit cooling_usage_vmean avail_vmean, lc(gs12) saving(cooling2, replace)
	
graph2tex, epsfile(sc_cool_avail)
	
twoway sc entertain_usage_vmean avail_vmean, mcolor("191 230 249") 	graphregion(c(white)) xscale(lc(gray)) yscale(lc(gray))	title("Entertainment", color(gray) size(medium)) ytitle(, c(gray) size(small)) xtitle("")/*"Years of electrification (mean by village)", c(gray) size(small))*/ ylabel(, labcolor(gray) tlc(gray) nogrid) xlabel(, labcolor(gray) tlc(gray) nogrid) ytitle("") /*"Number of Tier 1 appliances (mean by village)")*/ legend(off) || lfit entertain_usage_vmean avail_vmean, lc(gs12) saving(entertainment2, replace)

graph2tex, epsfile(sc_entertain_avail)
	
twoway sc housekeep_usage_vmean avail_vmean, mcolor("191 230 249") graphregion(c(white)) xscale(lc(gray)) yscale(lc(gray)) title("Housekeeping", color(gray) size(medium)) ytitle(, c(gray) size(small)) xtitle("")/*"Years of electrification (mean by village)", c(gray) size(small))*/ ylabel(, labcolor(gray) tlc(gray) nogrid) xlabel(, labcolor(gray) tlc(gray) nogrid) ytitle("") /*"Number of Tier 1 appliances (mean by village)")*/ legend(off) || lfit housekeep_usage_vmean avail_vmean, lc(gs12) saving(housekeeping2, replace)
	
graph2tex, epsfile(sc_housekeep_avail)
	
graph combine lighting2.gph cooling2.gph entertainment2.gph housekeeping2.gph, xcommon ycommon title("Appliance usage by availability of electricity among appliance groups", size(medium) c(gray)) graphregion(c(white)) l1("Hours of appliance usage per day (average by village)", size(small) color(gray)) b1("Availability of household electricity in hours per day (average by village)", size(small) color(gray))

graph2tex, epsfile(combo_avail)

* FIGURE 5: KEY APPLIANCE OWNERSHIP AND USAGE BY SOURCE 

preserve
label define source 0 "None" 1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup"
label values source
replace source = . if source == 0
graph bar (mean) new_led new_cfl new_tablefan new_ceiling new_cooler new_tv new_fridge, over(source, relabel(1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup") gap(130) axis(lcolor(gray)) label(labsize(small) labcolor(gray) tlcolor(gray))) bar(1, color("224 243 219")) bar(2, color("204 235 197")) bar(3, color("168 221 181")) bar(4, color("123 204 196")) bar(5, color("78 179 211"))	bar(6, color("43 140 190"))	bar(7, color("8 88 158")) graphregion(fcolor(white) color(white) lcolor(white)) plotregion(lcolor(white)) legend(lab(1 "LED bulb") lab(2 "CFL bulb") lab(3 "Table fan") lab(4 "Ceiling fan") lab(5 "Cooler") lab(6 "TV") lab(7 "Refrigerator") color(gray)) ylabel(, nogrid labcolor(gray) tlcolor(gray)) ytitle("Mean hours/day", color(gray) size(small)) legend(region(lcolor(white))) yscale(lcolor(gray)) bargap(10) legend(size(small) cols(3) rows(2)) title("Key appliance usage by primary electricity source",color(gray) size(medium)) blabel(,color(gray) size(vsmall)) saving(keyappsusage,replace) text(9.7 11 "HHs = 7,470", place(c) color(gray) size(small)) text(9.7 38 "HHs = 158", place(c) color(gray) size(small)) text(9.7 64 "HHs = 694", place(c) color(gray) size(small)) text(9.7 89 "HHs = 187", place(c) color(gray) size(small)) 
restore
* Creating ownership variables
gen own_led = 1 if q501_c_a_num_led > 0
replace own_led = 0 if own_led == .
gen own_cfl = 1 if q501_b_a_num_cfl > 0
replace own_cfl = 0 if own_cfl == .
gen own_tablefan = 1 if q501_g_a_num_tablefan > 0
replace own_tablefan = 0 if own_tablefan == .
gen own_ceiling = 1 if q501_f_a_num_ceilingfan > 0
replace own_ceiling = 0 if own_ceiling == .
gen own_cooler = 1 if q501_i_a_num_cooler > 0
replace own_cooler = 0 if own_cooler == .
gen own_tv = 1 if q501_h_a_num_tv > 0
replace own_tv = 0 if own_tv == .
gen own_fridge = 1 if q501_l_a_num_fridge > 0
replace own_fridge = 0 if own_fridge == .
* Graph production
preserve 
label define source 0 "None" 1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup"
label values source
replace source = . if source == 0
graph bar own_led own_cfl own_tablefan own_ceiling own_cooler own_tv own_fridge, over(source, relabel(1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup") gap(130) axis(lcolor(gray)) label(labsize(small) labcolor(gray) tlcolor(gray))) bar(1, color("191 129 45")) bar(2, color("223 194 125")) bar(3, color("246 232 195")) bar(4, color("199 234 229")) bar(5, color("128 205 193")) bar(6, color("53 151 143")) bar(7, color("1 102 94")) graphregion(fcolor(white) color(white) lcolor(white)) plotregion(lcolor(white)) legend(lab(1 "LED bulb") lab(2 "CFL bulb") lab(3 "Table fan") lab(4 "Ceiling fan") lab(5 "Cooler") lab(6 "TV") lab(7 "Refrigerator") color(gray)) ylabel(, nogrid labcolor(gray) tlcolor(gray)) ytitle("Percentage of households", color(gray) size(small)) legend(region(lcolor(white))) yscale(lcolor(gray)) bargap(10) legend(size(small) cols(3) rows(2)) title("Key appliance ownership by primary electricity source",color(gray) size(medium)) blabel(,color(gray) size(vsmall)) saving(keyappsown,replace) text(0.75 11 "HHs = 7,470", place(c) color(gray) size(small))	text(0.75 38 "HHs = 158", place(c) color(gray) size(small))	text(0.75 64 "HHs = 694", place(c) color(gray) size(small)) text(0.75 89 "HHs = 187", place(c) color(gray) size(small)) 
restore
graph combine keyappsown.gph keyappsusage.gph, c(1) r(2) graphregion(c(white)) 

*	***************************************************************************
*	2. MAIN ANALYSIS
*	***************************************************************************

* TABLE 3: OLS REGRESSION RESULTS FOR APPLIANCE CATEGORY OUTCOMES BY SOURCE
eststo: xi: reg lighting_usage grid mg shs backup higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)
estimates store ols4
eststo: xi: reg cooling_usage grid mg shs backup higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)
estimates store ols44
eststo: xi: reg entertain_usage grid mg shs backup higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code) 
estimates store ols444
eststo: xi: reg housekeep_usage grid mg shs backup higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)  
estimates store ols4444

*	*************************************************************************
*	3. APPENDIX FIGURES AND TABLES
*	*************************************************************************

* FIGURE S1: AVERAGE APPLIANCE USAGE BY STATE 

graph hbar (mean) lighting_usage cooling_usage entertain_usage housekeep_usage, over(q007_state, gap(130) axis(lcolor(gray)) label(labsize(small) labcolor(gray) tlcolor(gray))) bar(1, color("242 221 157")) bar(2, color("172 225 240")) bar(3, color("243 189 156")) bar(4, color("176 223 211")) graphregion(fcolor(white) color(white) lcolor(white)) plotregion(lcolor(white)) legend(lab(1 "Lighting") lab(2 "Cooling") lab(3 "Entertainment") lab(4 "Housekeeping") color(gray)) ylabel(, nogrid labcolor(gray) tlcolor(gray)) ytitle("Average hours of household appliance usage by state", color(gray) size(small)) legend(region(lcolor(white))) yscale(lcolor(gray)) bargap(10)legend(size(small)) title("Average household hours of usage by state", color(gray) size(medium)) blabel(, color(gray) size(vsmall))
	
* FIGURE S2: AVERAGE APPLIANCE USAGE BY ELECTRICITY SOURCE 

preserve
label define source 0 "None" 1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup"
label values source
replace source = . if source == 0
graph hbar (mean) lighting_usage cooling_usage entertain_usage housekeep_usage, over(source, relabel(1 "Grid" 2 "Mini-grid" 3 "SHS" 4 "Backup") gap(130) axis(lcolor(gray)) label(labsize(small) labcolor(gray) tlcolor(gray))) bar(1, color("242 221 157")) bar(2, color("172 225 240")) bar(3, color("243 189 156")) bar(4, color("176 223 211")) graphregion(fcolor(white) color(white) lcolor(white)) plotregion(lcolor(white))	legend(lab(1 "Lighting") lab(2 "Cooling") lab(3 "Entertainment") lab(4 "Housekeeping") color(gray)) ylabel(, nogrid labcolor(gray) tlcolor(gray)) ytitle("Average hours of household appliance usage by source", color(gray) size(small)) legend(region(lcolor(white))) yscale(lcolor(gray)) bargap(10)	legend(size(small)) title("Average household hours of usage by primary electricity source", color(gray) size(medium)) blabel(,color(gray) size(vsmall))
restore

* TABLE S3: ORDERED LOGISTIC REGRESSION RESULTS OF CATEGORIAL TIER OUTCOMES

eststo: ologit highest_tier grid mg shs backup, or allbaselevels vce(cluster q011_village_code)
estimates store ologit1 		
eststo: xi: ologit highest_tier grid mg shs backup i.q007_state, or allbaselevels vce(cluster q011_village_code)
estimates store ologit2
eststo: xi: ologit highest_tier grid mg shs backup higher_ed basic_ed ln_expend q204_adults q206_children q201_gender hindu sched_caste avail_vmean i.q007_state, or allbaselevels vce(cluster q011_village_code)
estimates store ologit3
eststo: xi: ologit highest_tier grid mg shs backup higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, or allbaselevels vce(cluster q011_village_code)
estimates store ologit4

* TABLE S4: OLS REGRESSION RESULTS OF APPLIANCE USAGE OUTCOMES BY STACKED SOURCES

eststo: xi: reg lighting_usage gridonly altonly gridalt higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)
estimates store stack4
eststo: xi: reg cooling_usage gridonly altonly gridalt higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)
estimates store stack44
eststo: xi: reg entertain_usage gridonly altonly gridalt higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code) 
estimates store stack444
eststo: xi: reg housekeep_usage gridonly altonly gridalt higher_ed basic_ed q204_adults q206_children q201_gender hindu sched_caste ln_expend avail_vmean ln_dist q218_land_ownership salaried i.q007_state, vce(cluster q011_village_code)    
estimates store stack4444
